package com.zhl.study.service.impl;

import com.zhl.study.service.IEmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Slf4j
@Service
public class EmailServiceImpl implements IEmailService {

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;


    @Override
    public void sendSimpleEmail(String to,String subject,String text) {
        // 定制纯文本邮件信息SimplemaileMassage
        SimpleMailMessage message = new SimpleMailMessage();
        //向指定的邮件发送信息
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        try {
            //发送邮件
            javaMailSender.send(message);
            log.info("纯文本邮件发送成功");
        }catch (MailException e) {
            log.error("纯文本邮件发送失败:{}", e.getMessage());
            e.printStackTrace();
        }
    }

    @Override
    public void sendComplexEmail(String to,String subject,String text,String filePath,String rscId,String rscPath) {
        // 定制复杂邮件信息M
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            // 使用MimeMailMessageHelper帮助类，并设置multipart多部件使用为true
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text,true);
            //设置邮件静态资源
            FileSystemResource res = new FileSystemResource(new File(rscPath));
            helper.addInline(rscId,res);
            // 设置邮件附件
            FileSystemResource file = new FileSystemResource(new File(filePath));
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
            helper.addAttachment(fileName, file);
            // 发送邮件
            javaMailSender.send(message);
            log.info("复杂邮件发送成功");
        }catch (MessagingException e) {
            log.error("复杂邮件发送失败:{}", e.getMessage());
            e.printStackTrace();
        }
    }


    public void sendTemplateEmail(String to, String subject, String content) {
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            // 使用MimeMessageHelper帮助类，并设置multipart多部件使用为true
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            // 发送邮件
            javaMailSender.send(message);
            System.out.println("模板邮件发送成功");
        } catch (MessagingException e) {
            System.out.println("模板邮件发送失败 "+e.getMessage());
            e.printStackTrace();}
    }


}
